const Router = require('koa-router')
// 引入配置koa-multer模块(用来处理图片上传)
const multer = require('koa-multer')
const path = require('path')
// 创建router
const filesRouter = new Router()
// 进行multer配置
let storage = multer.diskStorage({
    //文件保存路径
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname ,'../../public/uploads')) //路径一定要对
    },
    // 修改文件名称
    filename: function (req, file, cb) {
        let fileFormat = (file.originalname).split(".");
        cb(null,Date.now() + "." + fileFormat[fileFormat.length - 1]);
    }
})
//加载配置
let upload = multer({ storage: storage })
/**
 * @route POST api/files/upload
 * @description 上传图片
 * @access      接口公开
 */
filesRouter.post('/upload',upload.single('file'),async (ctx) =>{
    // 图片地址拼接
    const headPath = 'http://localhost:5000/uploads/' + ctx.req.file.filename
    ctx.body = {
        filename: headPath,//返回文件名
        body:ctx.req.body,
        status:200,
        success:true,
    }
})
module.exports = filesRouter